package controller;


import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modal.ProductModal;
import modal.UserModal;
import dao.BidsDAO;
import dao.ProductsDAO;

/**
 * Servlet implementation class HomeController
 */
@WebServlet("/HomeController")

public class HomeController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor. 
	 */
	public HomeController() {

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession();
			if (session.getAttribute("user") == null){
				response.sendRedirect("/BidMarket/login.jsp");
			}
			else{
				UserModal user= (UserModal) session.getAttribute("user");
				int userID = user.getUserId();

				BidsDAO bDao = new BidsDAO();			
				ArrayList<Integer> prodIDs =new ArrayList<Integer>(bDao.getProductIdOfActiveBidsToBuy(userID));
				bDao.disconnnect();	

				ArrayList<String>productsOfBidList =new ArrayList<String>();
				for(int i=0; i<prodIDs.size(); i++){
					ProductsDAO pDao = new ProductsDAO();	
					ProductModal p= pDao.getProductwithProductId(prodIDs.get(i));
					if(p.getStatus()==0){
						productsOfBidList.add(""+p.getProductId());
						productsOfBidList.add(p.getPhotoUrl());
						productsOfBidList.add(p.getName());
						productsOfBidList.add(p.getDescription());
						productsOfBidList.add(p.getPrice());
					}
					pDao.disconnnect();
				}
				
				ProductsDAO pDao = new ProductsDAO();
				ArrayList<ProductModal> products =new ArrayList<ProductModal>(pDao.getProductwithOwnerId(userID));
				pDao.disconnnect();
				ArrayList<String>myProducts =new ArrayList<String>();
				ArrayList<String>mySoldProducts =new ArrayList<String>();
				for(int i=0; i<products.size(); i++){
					if(products.get(i).getStatus()==0){
						myProducts.add(""+products.get(i).getProductId());
						myProducts.add(products.get(i).getPhotoUrl());
						myProducts.add(products.get(i).getName());
						myProducts.add(products.get(i).getDescription());
						myProducts.add(products.get(i).getPrice());
					}
					else{
						mySoldProducts.add(""+products.get(i).getProductId());
						mySoldProducts.add(products.get(i).getPhotoUrl());
						mySoldProducts.add(products.get(i).getName());
						mySoldProducts.add(products.get(i).getDescription());
						mySoldProducts.add(products.get(i).getPrice());

					}
				}
				session.setAttribute("productsOfBidList", productsOfBidList);
				session.setAttribute("myProducts", myProducts);		
				session.setAttribute("mySoldProducts", mySoldProducts);	
				request.getRequestDispatcher("/index.jsp").forward(request, response);	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}













